package com.zqxq.lottery.mq;

import com.zqxq.lottery.common.constant.HuaYangConstant;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.controller.order.BonusCalcController;
import com.zqxq.lottery.entity.bo.sys.SysActivityBO;
import com.zqxq.lottery.entity.enums.ActivityIdEnum;
import com.zqxq.lottery.entity.po.user.UserMsgPO;
import com.zqxq.lottery.mapper.user.UserInfoMapper;
import com.zqxq.lottery.service.order.IUserOrderService;
import com.zqxq.lottery.service.sys.ISysActivityService;
import com.zqxq.lottery.service.user.IUserInfoService;
import com.zqxq.lottery.service.user.IUserMsgService;
import com.zqxq.lottery.util.JPushUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 说    明：   消息通知处理
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/5/29 16:19
 * 修 改 人：
 * 修改日期：
 **/
@Component
@Slf4j
public class Receiver {
    @Autowired
    private ISysActivityService sysActivityService;
    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private IUserMsgService userMsgService;
    @Autowired
    private YmlConfig ymlConfig;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BonusCalcController bonusCalcController;
    @Autowired
    private Sender sender;
    @RabbitListener(queues = HuaYangConstant.MQ_SEND_COUPON)
    public void sendUserCoupon(String userId){
        try {
            SysActivityBO sysActivityBO = new SysActivityBO();
            sysActivityBO.setActivityId(ActivityIdEnum.REGISTER.getId());
            sysActivityBO.setUserId(Long.valueOf(userId));
            sysActivityService.saveUserActivity(sysActivityBO);
        }catch (Exception e){
            log.error("发放优惠券异常",e);
        }
        log.info("Send coupon:"+userId);
    }

    @RabbitListener(queues = HuaYangConstant.MQ_PUSH_LOTTERY_RESULT)
    public void pushResult(String msg) {
        try {
            //推送消息加入内部消息
            userMsgService.insertBatchMsg(msg);
            log.info("数字竞猜站内通知："+msg);
            //开奖结果信息通知
            Map<String,Object> map =  JPushUtil.pushToAll(msg);
            log.info("push flag:"+map.get("flag")+"");
        }catch (Exception e){
            log.error("开奖通知推送失败",e);
        }
        log.info("pushResult "+msg);

    }

    @RabbitListener(queues = HuaYangConstant.MQ_PUSH_JCZQ_RESULT)
    public void pushJczqResult(String msg) {
        try {
            //极光推送开奖信息 足彩开奖格式： 标题：足彩开奖了，你中奖没？；内容：今日开奖金额XXX.XX元，共有XXX人中奖，最多中得XX元
            Map<String,Object> map = userOrderService.selectPrizeInfo();
            if (map!=null && map.get("totalPrize")!=null && map.get("prizeNum")!= null && map.get("maxPrize")!=null) {
                String content = "今日开奖金额" + map.get("totalPrize") + "元，共有" + map.get("prizeNum") + "人中奖，最多中得" + map.get("maxPrize") + "元";
                //推送消息加入内部消息
                userMsgService.insertBatchMsg(content);

                JPushUtil.pushToAll(content);
            }
        }catch (Exception e){
            log.error("足彩中奖通知推送失败",e);
        }
        log.info("pushJczqResult "+msg);

    }

    @RabbitListener(queues = HuaYangConstant.MQ_BATCH_INSERT_MSG)
    public void batchInsertMsg(String msg){
        try {
            log.info("rabbitmq批量插入用户消息开始");
            //内部消息通知已下单用户
            List<Long> userIds = userInfoMapper.selectOrderUserList();
            List<UserMsgPO> list = new ArrayList<>();
            for (Long userId : userIds) {
                UserMsgPO userMsgPO = new UserMsgPO();
                userMsgPO.setType(4);
                userMsgPO.setContent(msg);
                userMsgPO.setUser_id(userId);
                userMsgPO.setId(ymlConfig.getPkId());
                list.add(userMsgPO);
            }
            userMsgService.insertBatch(list);
        }catch (Exception e){
            log.error("批量插入用户消息异常",e);
        }
        log.info("批量插入用户消息成功");
    }
    /*******************************开奖*******************************/
    @RabbitListener(queues = HuaYangConstant.MQ_OPENPRIZE_BET_FC)
    public void openPrizeBetFc(String lotteryId){
        try {
            bonusCalcController.lotteryBonusCalc(12L);
            sender.send(HuaYangConstant.MQ_OPENPRIZE_BET_DLT,"14",1000*60*1);
        }catch (Exception e){
            log.error("福彩3D开奖异常",e);
        }
        log.info("openPrizeBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_OPENPRIZE_BET_DLT)
    public void openPrizeBetDlt(String lotteryId){
        try {
            bonusCalcController.lotteryBonusCalc(14L);
            sender.send(HuaYangConstant.MQ_OPENPRIZE_BET_QXC,"15",1000*60*1);
        }catch (Exception e){
            log.error("大乐透开奖异常",e);
        }
        log.info("openPrizeBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_OPENPRIZE_BET_QXC)
    public void openPrizeBetQxc(String lotteryId){
        try {
            bonusCalcController.lotteryBonusCalc(15L);
            sender.send(HuaYangConstant.MQ_OPENPRIZE_BET_PL,"16",1000*60*1);
        }catch (Exception e){
            log.error("七星彩开奖异常",e);
        }
        log.info("openPrizeBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_OPENPRIZE_BET_PL)
    public void openPrizeBetPl(String lotteryId){
        try {
            bonusCalcController.lotteryBonusCalc(16L);
            sender.send(HuaYangConstant.MQ_OPENPRIZE_BET_JCZQ,"21",1000*60*1);
        }catch (Exception e){
            log.error("排列3开奖异常",e);
        }
        log.info("openPrizeBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_OPENPRIZE_BET_JCZQ)
    public void openPrizeBetJczq(String lotteryId){
        try {
            bonusCalcController.lotteryBonusCalc(21L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_SSQ,"11",1000*60*5);
        }catch (Exception e){
            log.error("竞猜足球开奖异常",e);
        }
        log.info("openPrizeBet:"+lotteryId);
    }
    /*******************************派奖*******************************/
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_SSQ)
    public void awardBetSsq(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(11L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_FC,"12",1000*60*1);
        }catch (Exception e){
            log.error("双色球派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_FC)
    public void awardBetFc(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(12L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_DLT,"14",1000*60*1);
        }catch (Exception e){
            log.error("福彩3D派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_DLT)
    public void awardBetDlt(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(14L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_QXC,"15",1000*60*1);
        }catch (Exception e){
            log.error("大乐透派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_QXC)
    public void awardBetQxc(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(15L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_PL,"16",1000*60*1);
        }catch (Exception e){
            log.error("七星彩派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_PL)
    public void awardBetPl(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(16L);
            sender.send(HuaYangConstant.MQ_AWARD_BET_JCZQ,"21",1000*60*1);
        }catch (Exception e){
            log.error("排列3派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_JCZQ)
    public void awardBetJczq(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(21L);
        }catch (Exception e){
            log.error("竞猜足球派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_JCLQ)
    public void awardBetJclq(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(24L);
        }catch (Exception e){
            log.error("竞猜篮球派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
    @RabbitListener(queues = HuaYangConstant.MQ_AWARD_BET_DJ)
    public void awardBetDj(String lotteryId){
        try {
            bonusCalcController.lotteryBonusPrize(10L);
        }catch (Exception e){
            log.error("电竞派奖异常",e);
        }
        log.info("awardBet:"+lotteryId);
    }
}
